package com.example.learn_spring_boot3.config;

import com.example.learn_spring_boot3.interecepror.JWTInterceptor;
import com.example.learn_spring_boot3.interecepror.PerformanceInterceptor;
import com.example.learn_spring_boot3.interecepror.TestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public JWTInterceptor jwtInterceptor() {
        return new JWTInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TestInterceptor());
        registry.addInterceptor(new PerformanceInterceptor());

        // 添加 LoginInterceptor 拦截器, 只针对所有 user 接口生效
        // addPathPatterns: 表示拦截所有的请求
        // registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/user/**");

        // 这里不要直接 new JWTInterceptor() 的方式使用, 因为 JWTInterceptor 里面需要用到自动注入
        // 不然会无法注入, 采用 @Bean 的方式
        registry.addInterceptor(jwtInterceptor())
                .addPathPatterns("/**") // 默认拦截全部的请求
                .excludePathPatterns( // 指定的接口不需要拦截
                        "/user/register",
                        "/user/login",
                        "/user/logout",
                        "**/export",
                        "**/import",
                        "/file/download/**",
                        "/http/**"
                );
    }
}
